//
//  MemberList.m
//  LuQu
//
//  Created by xp on 2017/9/29.
//  Copyright © 2017年 mingthink. All rights reserved.
//

#import "MemberListService.h"
#import "SQLManager.h"
@interface MemberListService() {
    FMDatabaseQueue *_dbQueue;
}

@end
@implementation MemberListService
- (MemberListService *)init {
    if (self = [super init]) {
        _dbQueue = [[SQLManager sharedInstance] dbQueue];
    }
    return self;
}

#pragma mark 插入新的数据，会替换原来的数据
-(void)insert:(SBMember *)member {
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{
        [_dbQueue inDatabase:^(FMDatabase *db) {
            FMResultSet *rs =[db executeQuery:@"SELECT COUNT(UserId) AS countNum FROM MemberList WHERE UserId = ?",member.UserId];
            while ([rs next]) {
                NSInteger count = [rs intForColumn:@"countNum"];
                if (count > 0) {
                    //存在
                  [db executeUpdate:@"update MemberList set AccessToken=?, RefreshToken=?, NickName=?, SpaceBackGround=?, Portrait=?, Sex=?, Birthday=?, Mobile=?, QQ=?, Weixin=?, AreaCode=?, ProvinceName=?, CityName=?, DistrictsName=?, IsAuthentication=?, IsMember=?, IsBindingStudent=?, HxTag=?, IsTeacher=?, IsVisitor=? where UserId=?", member.AccessToken, member.RefreshToken, member.NickName, member.SpaceBackGround, member.Portrait, member.Sex, member.Birthday, member.Mobile, member.QQ, member.Weixin, member.AreaCode, member.ProvinceName, member.CityName, member.DistrictsName, [NSString stringWithFormat:@"%d",member.IsAuthentication],[NSString stringWithFormat:@"%d",member.IsMember], [NSString stringWithFormat:@"%d",member.IsBindingStudent], member.HxTag, [NSString stringWithFormat:@"%d",member.IsTeacher],[NSString stringWithFormat:@"%d",member.IsVisitor],member.UserId];
                    
                }
                else
                {
                    //不存在
                    [db executeUpdate:@"insert into MemberList (AccessToken, RefreshToken, UserId, NickName, SpaceBackGround, Portrait, Sex, Birthday, Mobile, QQ, Weixin, AreaCode, ProvinceName, CityName, DistrictsName, IsAuthentication, IsMember, IsBindingStudent, HxTag, IsTeacher, IsVisitor) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", member.AccessToken, member.RefreshToken, member.UserId, member.NickName, member.SpaceBackGround, member.Portrait, member.Sex, member.Birthday, member.Mobile, member.QQ, member.Weixin, member.AreaCode, member.ProvinceName, member.CityName, member.DistrictsName, [NSString stringWithFormat:@"%d",member.IsAuthentication],[NSString stringWithFormat:@"%d",member.IsMember], [NSString stringWithFormat:@"%d",member.IsBindingStudent], member.HxTag, [NSString stringWithFormat:@"%d",member.IsTeacher], [NSString stringWithFormat:@"%d",member.IsVisitor]];
                }
            }
             [rs close];
            //[db executeUpdate:@"delete from MemberList"];

        }];
    });
}
-(void)update:(SBMember *)member{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{
        [_dbQueue inDatabase:^(FMDatabase *db) {
            [db executeUpdate:@"update MemberList set NickName=?, SpaceBackGround=?, Portrait=?, Sex=?, Birthday=?, Mobile=?, QQ=?, Weixin=?, AreaCode=?, ProvinceName=?, CityName=?, DistrictsName=?, IsAuthentication=?, IsMember=?, IsBindingStudent=?, HxTag=?, IsTeacher=? where UserId=?", member.NickName, member.SpaceBackGround, member.Portrait, member.Sex, member.Birthday, member.Mobile, member.QQ, member.Weixin, member.AreaCode, member.ProvinceName, member.CityName, member.DistrictsName, [NSString stringWithFormat:@"%d",member.IsAuthentication],[NSString stringWithFormat:@"%d",member.IsMember], [NSString stringWithFormat:@"%d",member.IsBindingStudent], member.HxTag, [NSString stringWithFormat:@"%d",member.IsTeacher],member.UserId];
        }];
    });
}
//-(BOOL)queryIsExistMember:(int)memberType{
//    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{
//        [_dbQueue inDatabase:^(FMDatabase *db) {
//            FMResultSet *rs =[db executeQuery:@"SELECT COUNT(IsVisitor) AS countNum FROM MemberList WHERE IsVisitor = ?",[NSString stringWithFormat:@"%d",memberType]];
//            while ([rs next]) {
//                NSInteger count = [rs intForColumn:@"countNum"];
//                if (count > 0) {
//                    //存在
//                    return YES;
//
//                }
//                else
//                {
//                    //不存在
//                    return NO;
//                }
//            }
//
//            [rs close];
//
//         }];
//            //[db executeUpdate:@"delete from MemberList"];
//    });
//         return NO;
//}
#pragma mark 查询最新插入的Member
- (void)queryLastOneWithCallBack:(void (^)(SBMember *))block {
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{
        __block SBMember *member = nil;
        [_dbQueue inDatabase:^(FMDatabase *db) {
            FMResultSet *rs = [db executeQuery:@"SELECT * FROM MemberList order by _id desc limit 1"];
            if ([rs next]) {
                member = [[SBMember alloc] init];
                
                //member.Token = [rs stringForColumn:@"Token"];
                member.AccessToken = [rs stringForColumn:@"AccessToken"];
                member.RefreshToken = [rs stringForColumn:@"RefreshToken"];
                
                member.UserId = [rs stringForColumn:@"UserId"];
                member.NickName = [rs stringForColumn:@"NickName"];
                member.SpaceBackGround = [rs stringForColumn:@"SpaceBackGround"];
                member.Portrait = [rs stringForColumn:@"Portrait"];
                member.Sex = [rs stringForColumn:@"Sex"];
                member.Birthday = [rs stringForColumn:@"Birthday"];
                
                member.Mobile = [rs stringForColumn:@"Mobile"];
                member.QQ = [rs stringForColumn:@"QQ"];
                member.Weixin = [rs stringForColumn:@"Weixin"];
                
                member.AreaCode = [rs stringForColumn:@"AreaCode"];
                member.ProvinceName = [rs stringForColumn:@"ProvinceName"];
                member.CityName = [rs stringForColumn:@"CityName"];
                member.DistrictsName = [rs stringForColumn:@"DistrictsName"];
                
                member.IsAuthentication = [[rs stringForColumn:@"IsAuthentication"] intValue];
                member.IsMember = [[rs stringForColumn:@"IsMember"] intValue];
                member.IsBindingStudent = [[rs stringForColumn:@"IsBindingStudent"] intValue];
                member.HxTag = [rs stringForColumn:@"HxTag"];
                
                member.IsTeacher = [[rs stringForColumn:@"IsTeacher"] intValue];
                member.IsVisitor = [[rs stringForColumn:@"IsVisitor"] intValue];
            }
            [rs close];
        }];
        dispatch_async(dispatch_get_main_queue(), ^{
            block(member);
        });
    });
}
- (void) queryAllWithCallBack:(void (^)(NSMutableArray<SBMember *> *members))block {
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{
        __block SBMember *member = nil;
        NSMutableArray *memberArr=[[NSMutableArray alloc]init];
        [_dbQueue inDatabase:^(FMDatabase *db) {
            FMResultSet *rs = [db executeQuery:@"SELECT * FROM MemberList order by _id desc"];
            while([rs next]) {
                member = [[SBMember alloc] init];
                
                //member.Token = [rs stringForColumn:@"Token"];
                member.AccessToken = [rs stringForColumn:@"AccessToken"];
                member.RefreshToken = [rs stringForColumn:@"RefreshToken"];
                
                member.UserId = [rs stringForColumn:@"UserId"];
                member.NickName = [rs stringForColumn:@"NickName"];
                member.SpaceBackGround = [rs stringForColumn:@"SpaceBackGround"];
                member.Portrait = [rs stringForColumn:@"Portrait"];
                member.Sex = [rs stringForColumn:@"Sex"];
                member.Birthday = [rs stringForColumn:@"Birthday"];
                
                member.Mobile = [rs stringForColumn:@"Mobile"];
                member.QQ = [rs stringForColumn:@"QQ"];
                member.Weixin = [rs stringForColumn:@"Weixin"];
                
                member.AreaCode = [rs stringForColumn:@"AreaCode"];
                member.ProvinceName = [rs stringForColumn:@"ProvinceName"];
                member.CityName = [rs stringForColumn:@"CityName"];
                member.DistrictsName = [rs stringForColumn:@"DistrictsName"];
                
                member.IsAuthentication = [[rs stringForColumn:@"IsAuthentication"] intValue];
                member.IsMember = [[rs stringForColumn:@"IsMember"] intValue];
                member.IsBindingStudent = [[rs stringForColumn:@"IsBindingStudent"] intValue];
                member.HxTag = [rs stringForColumn:@"HxTag"];
                
                member.IsTeacher = [[rs stringForColumn:@"IsTeacher"] intValue];
               member.IsVisitor = [[rs stringForColumn:@"IsVisitor"] intValue];
                [memberArr addObject:member];
            }
            [rs close];
        }];
        dispatch_async(dispatch_get_main_queue(), ^{
            block(memberArr);
        });
    });
}

@end
